package com.xmb.wechat.util;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.provider.MediaStore;
import android.text.TextUtils;

import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dmcbig.mediapicker.PickerActivity;
import com.dmcbig.mediapicker.PickerConfig;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.PYQPicBean;
import com.xmb.wechat.definterface.PermissionRequestCallBack;
import com.xmb.wechat.definterface.PicChooseCallBack;
import com.xmb.wechat.definterface.PicChooseWithCutCallBack;
import com.xmb.wechat.definterface.PicChooseWithOutCutCallBack;

import java.util.List;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/23 11:32
 */
public class PicChooseUtils {

    /**
     * 选择一张图片（剪切/不规定裁剪比例）
     *
     * @param activity
     */
    public static void chooseOnePicWithCut(Activity activity, PicChooseWithCutCallBack callBack) {
        choosePic(activity, BaseActivity.REQUEST_CODE_CHOOSE_PIC_WITH_CUT, callBack, 0, 0);
    }

    /**
     * 选择一张图片（剪切/规定裁剪比例）
     *
     * @param activity
     */
    public static void chooseOnePicWithCut(Activity activity, float ratioX, float ratioY, PicChooseWithCutCallBack callBack) {
        choosePic(activity, BaseActivity.REQUEST_CODE_CHOOSE_PIC_WITH_CUT, callBack, ratioX, ratioY);
    }

    /**
     * 选择一张图片（不剪切）
     *
     * @param activity
     */
    public static void chooseOnePicWithOutCut(Activity activity, PicChooseWithOutCutCallBack callBack) {
        choosePic(activity, BaseActivity.REQUEST_CODE_CHOOSE_PIC_WITHOUT_CUT, callBack, 0, 0);
    }

    private static void choosePic(Activity activity, int requestCode, PicChooseCallBack callBack, float ratioX, float ratioY) {
        doPermissionsRequest(new PermissionRequestCallBack() {
            @Override
            public void onSuccess() {
                try {
                    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    activity.startActivityForResult(intent, requestCode);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onDenied() {
                ToastUtils.showShort("权限未开启");
            }
        }, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE});
        //设置回调
        try {
            BaseActivity act = (BaseActivity) activity;
            act.setPicCallBack(callBack, ratioX, ratioY);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 选择多张图片
     *
     * @param activity
     * @param requestCode
     * @param data
     */
    public static void choosePyqPics(Activity activity, int requestCode, List<PYQPicBean> data) {
        doPermissionsRequest(new PermissionRequestCallBack() {
            @Override
            public void onSuccess() {
                int count;
                if (TextUtils.isEmpty(data.get(data.size() - 1).getPic())) {
                    count = 9 - data.size() + 1;
                } else {
                    count = 9 - data.size();
                }
                Intent intent = new Intent(activity, PickerActivity.class);
                intent.putExtra(PickerConfig.SELECT_MODE, PickerConfig.PICKER_IMAGE);
                intent.putExtra(PickerConfig.MAX_SELECT_COUNT, count);//最大选择数量，默认40（可选）
                activity.startActivityForResult(intent, requestCode);
            }

            @Override
            public void onDenied() {
                ToastUtils.showShort("权限未开启");
            }
        }, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE});
    }


    /**
     * 权限申请
     *
     * @param callBack
     * @param permissions
     */
    @SuppressLint("WrongConstant")
    private static void doPermissionsRequest(final PermissionRequestCallBack callBack, String[] permissions) {
        if (!PermissionUtils.isGranted(permissions)) {
            PermissionUtils.permission(permissions).callback(new PermissionUtils.FullCallback() {

                @Override
                public void onGranted(List<String> permissionsGranted) {
                    callBack.onSuccess();
                }

                @Override
                public void onDenied(List<String> permissionsDeniedForever, List<String> permissionsDenied) {//权限回调规律,全部请求完才进回调
                    if (permissionsDeniedForever.size() > 0) {
                        ToastUtils.showShort("检测到有权限未开启,部分功能可能无法使用");
                    }
                }
            }).request();
        } else {
            callBack.onSuccess();
        }
    }
}
